package com.gxuwz.library_consumer.controller;

import com.github.pagehelper.PageInfo;
import com.gxuwz.commonmodule.entity.Book;
import com.gxuwz.commonmodule.entity.BorrowRecord;
import com.gxuwz.library_consumer.service.BorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDate;
import java.util.List;

@Controller
@RequestMapping("/borrow")
public class BorrowController {
    private String view_list = "borrow/borrow_list";//管理员信息列表
    private String view_input = "borrow/borrow_add";//管理员读者信息
    private String view_edit = "borrow/borrow_edit";//编辑管理员信息
    private String view_edits = "borrow/borrow_edits";//编辑管理员信息
    private String redirect_list = "redirect:/borrow/findPage?pageIndex=1";

    @Autowired
    private BorrowService borrowService;

    /**
     * 借阅书籍
     * @param bookID
     * @return
     */
    @PostMapping("/borrowBook")
    public String borrowBook(String bookID){
        Book book = bookService.findById(bookID);
//        HttpSession session = request.getSession();
//        String username = (String) session.getAttribute("account");
//        String name = (String) session.getAttribute("account");
        BorrowRecord borrowRecord = new BorrowRecord();
        borrowRecord.setBookID(book.getBookID());
        borrowRecord.setTitle(book.getTitle());
//        borrowRecord.setUsername(username);
//        borrowRecord.setName(name);
        borrowRecord.setBorrowDate(LocalDate.now());
        borrowRecord.setReturnedStatus("未归还");
        bookService.borrowAdd(borrowRecord);
//        model.addAttribute("p",book);
        System.out.println("借阅成功");
        return redirect_borrowList;
    }

    /**
     * 借阅记录首页
     * @return
     */
    @RequestMapping("/borrowMain")
    public String toBorrowMain(){
        return "borrow/borrow_list";
    }

    /**
     * 借阅记录信息列表
     * @return
     */
    @GetMapping("/borrowList")
    public ModelAndView listBorrow(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("borrow/borrow_list");
        List<BorrowRecord> borrowRecordList = bookService.borrowList(null);
        modelAndView.addObject("borrowRecordList",borrowRecordList);
        return modelAndView;
    }

    /**
     * 跳入增加借阅记录视图
     * @return
     */
    @RequestMapping("/borrowInput")
    public String borrowInput(){
        return "borrow/borrow_add";
    }

    /**
     * 增加借阅记录信息
     * @param borrowRecord
     * @return
     */
    @PostMapping("/borrowAdd")
    public String addBorrow(BorrowRecord borrowRecord){
        bookService.borrowAdd(borrowRecord);
        return redirect_borrowList;
    }

    /**
     * 通过ID 删除借阅记录信息
     * @param borrowID
     * @return
     */
    @GetMapping("/deleteBorrow")
    public String deleteBorrow(Integer borrowID){
        bookService.deleteBorrowRecord(borrowID);
        System.out.println("删除成功");
        return redirect_list;
    }

    /**
     * 跳转到编辑借阅记录视图
     * @param borrowID
     * @param model
     * @return
     */
    @GetMapping("/borrowGet")
    public String getBorrow(Integer borrowID, Model model){
        BorrowRecord borrowRecord = bookService.findByIds(borrowID);
        model.addAttribute("m",borrowRecord);
        return "borrow/borrow_edit";
    }

    /**
     * 编辑借阅记录信息
     * @param borrowRecord
     * @return
     */
    @PostMapping("/borrowUpdate")
    public String updateBorrow(BorrowRecord borrowRecord){
        bookService.updateBorrowRecord(borrowRecord);
        return redirect_borrowList;
    }

    /**
     * 分页查询（不带条件）
     * @param model
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @GetMapping("/findPages")
    public String findAllBorrow(Model model, @RequestParam(value = "pageIndex",defaultValue = "1")int pageIndex,
                                @RequestParam(value = "pageSize",defaultValue = "4",required = false)int pageSize){
        PageInfo page = bookService.findPage(pageIndex,pageSize);
        model.addAttribute("path","/book/findPages?pageIndex=");
        model.addAttribute("page",page);
        return "borrow/borrow_list";
    }
}
